<?php

namespace App\Api\Dao;

use App\Models\TongUserAddress;
use App\Models\TongUserAreaLog;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class UserAreaLogDao extends BaseDao
{

    /**
     * @param $data
     * @return \Illuminate\Database\Eloquent\Builder
     */
    function getBuildQuery($data)
    {
        // TODO: Implement getBuildQuery() method.
        $query= TongUserAreaLog::query();

        if(checkIsHasField($data,'id')){
            $query->where('id',$data['id']);
        }
        if(checkIsHasField($data,'u_id')){
            $query->where('u_id',$data['u_id']);
        }
        return $query;
    }

    /**
     * @param array $data
     * @return array
     */
    public function getUserArea(array $data)
    {
        $query = $this->getBuildQuery($data);
        if(!$query->exists()){
            return [];
        }
        $result = $query->get()->each(function($item, $key){
            if(!empty($item['province_id'])){
                $item['province_name'] = DcatDistpickerHelper::getAreaName($item['province_id']);
            }
            if(!empty($item['city_id'])){
                $item['city_name'] = DcatDistpickerHelper::getAreaName($item['city_id']);
            }
            if(!empty($item['area_id'])){
                $item['area_name'] = DcatDistpickerHelper::getAreaName($item['area_id']);
            }
        });
        return $result;
    }
}
